package com.grad.webstudy.controller;

import com.github.pagehelper.PageInfo;
import com.grad.webstudy.annota.ApiAuthorization;
import com.grad.webstudy.constants.AuthConstants;
import com.grad.webstudy.constants.CourseTypeConstants;
import com.grad.webstudy.model.CourseType;
import com.grad.webstudy.service.CourseTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: zjf
 * @Date: 2020/2/1 13:49.
 */
@RestController
@RequestMapping("/courseType")
@Validated
public class CourseTypeController {


    @Autowired
    private CourseTypeService courseTypeService;


    @PostMapping("/addType")
    @ApiAuthorization(auth = AuthConstants.COURSE_TYPE_ADD)
    public boolean addType(String typeName, @RequestParam(defaultValue = CourseTypeConstants.PARENT_ID_STRING) int parentId, @Max(1) @Min(0) int type) {
        if (type == CourseTypeConstants.TYPE_DIRECT) {
            parentId = CourseTypeConstants.PARENT_ID;
        }
        return courseTypeService.addCourseType(typeName, parentId, type);
    }

    @DeleteMapping("/deleteTypes")
    @ApiAuthorization(auth = AuthConstants.COURSE_TYPE_DELETE)
    public boolean deleteTypes(String ids) {
        return courseTypeService.deleteCourseType(ids);
    }

    @PutMapping("/update")
    @ApiAuthorization(auth = AuthConstants.COURSE_TYPE_UPDATE)
    public boolean updateCourseType(int id, String typeName,
                                    @RequestParam(defaultValue = CourseTypeConstants.PARENT_ID_STRING) int parentId,
                                    @Max(1) @Min(0) int type) {
        if (type == CourseTypeConstants.TYPE_DIRECT) {
            parentId = CourseTypeConstants.PARENT_ID;
        }
        return courseTypeService.updateCourseType(new CourseType.Builder()
                .setId(id)
                .setTypeName(typeName)
                .setParentId(parentId)
                .setType(type).build());
    }


    /**
     * 分页查询全部信息
     *
     * @param currPage
     * @param pageSize
     * @return
     */
    @GetMapping("/queryByPage")
    public PageInfo selectAllCourseType(@RequestParam(defaultValue = "1") int currPage,
                                        @RequestParam(defaultValue = "5") int pageSize,
                                        Integer type,
                                        String keyword) {
        return courseTypeService.queryAllCourseType(currPage, pageSize, type, keyword.trim());
    }

    /**
     * 获取方向下的分类
     *
     * @param parentId
     * @return
     */
    @GetMapping("/queryChild")
    public List<CourseType> queryChildType(Integer parentId) {
        return courseTypeService.queryChild(parentId);
    }


    /**
     * 获取全部方向
     *
     * @return
     */
    @GetMapping("/queryDirect")
    public List<CourseType> queryDirect() {
        return courseTypeService.queryDire();
    }


    /**
     * 不分页查询全部信息
     *
     * @return
     */
    @GetMapping("/queryAll")
    public List<CourseType> queryAll() {

        List<CourseType> result=new ArrayList<>();

        result.addAll(courseTypeService.queryDire());
        result.addAll(courseTypeService.queryAllChild());
        return result;
    }



    @GetMapping("/queryAllChild")
    public List<CourseType> queryAllChild(){
        return courseTypeService.queryAllChild();
    }


}
